<?php

namespace App\Http\Controllers\Api;


use App\Models\Role;
use App\Models\Character;
use App\Models\CartoonRoles;
use App\Models\CartoonMembers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use App\Validate\api\CharacterValidate;
use App\Common\ConstCode;
/**
 * Class CharacterController 漫画角色控制器
 * @package App\Http\Controllers\Api
 */
class CharacterController extends BaseController
{
    public $successStatus=200;
    protected $role;
    protected $character;
    protected $v;
    /**
     * api注册
     * @param Request $request
     * @return array|\Illuminate\Http\JsonResponse
     */

    public function __construct(Role $role,Character $character,CharacterValidate $v)
    {
        $this->role = $role;
        $this->character = $character;
        $this->v = $v;
    }



    public function create(Request $request){
        $this->v->validate('character');
        $data=$request->input();
        $data['uid']=Auth::guard('api')->user()->member_id;
        unset($data['s']);
        //去除链接
        $data['face_img']=str_replace(env('OSS_URL',''),'',$data['face_img']);
        $data['after_hair_img']=str_replace(env('OSS_URL',''),'',$data['after_hair_img']);
        $data['front_hair_img']=str_replace(env('OSS_URL',''),'',$data['front_hair_img']);
        $data['expression_img']=str_replace(env('OSS_URL',''),'',$data['expression_img']);
        $data['cloth_img']=str_replace(env('OSS_URL',''),'',$data['cloth_img']);
        $bool=$this->character->insert($data);
        if($bool){
            return response()->json(['code'=>1,'msg'=>'添加成功']);
        }else{
            return response()->json(['code'=>0,'msg'=>'添加失败']);
        }
        
    }


    public function info(Request $request){
        $this->v->validate('info');
        $data=$request->input();
        $res=$this->character->info($data);
        if($res){
            return response()->json(['code'=>1,'data'=>$res]);
        }else{
            return response()->json(['code'=>0,'msg'=>'查询失败']);
        }
    }

    public function edit(Request $request){
        $this->v->validate('edit');
        $data=$request->input();
        $bool=$this->character->edit($data);
        if($bool){
            return response()->json(['code'=>1,'msg'=>'修改成功']);
        }else{
            return response()->json(['code'=>0,'msg'=>'修改失败']);
        }
    }

    public function del(Request $request){
        $this->v->validate('info');
        $id=$request->input('id','');
        $bool=$this->character->destroy($id);
        if($bool){
            return response()->json(['code'=>1,'msg'=>'删除成功']);
        }else{
            return response()->json(['code'=>0,'msg'=>'删除失败']);
        }
    }

    public function source(Request $request){
        $this->v->validate('source');
        $data=$request->input();
        $data['page'] = $request->input('page',ConstCode::PAGE);
        $data['pageSize']  = $request->input('pageSize',ConstCode::PER_PAGE);
        $data['tag']=$request->input('tag','');
        $data['title']=$request->input('title','');
        $data['location']  = $request->input('location',1);
        $data['uid']=Auth::guard('api')->user()->member_id;
        $result=$this->role->c_lists($data);
        if($result){
            return response()->json(['code'=>1,'data'=>$result]);
        }else{
            return response()->json(['code'=>0,'msg'=>'添加失败']);
        }
    }



    /**
     * 漫画项目角色列表
     * @param Request $request
     * @return array
     */
    public function getCartoonRoleList(Request $request){
        $this->v->validate('getCartoonRoleList');
        $info=$request->input();
        $CartoonRoles=new CartoonRoles();
        $list=$CartoonRoles->getJoinList(['cartoon_id'=>$info['cartoon_id']],$info['pageSize'],'characters.id','desc');
        $data['total']=$list['total'];
        $data['list']=$list['data'];
        if (empty($list['data'])){
            return response()->json(['code'=>1,'msg'=>'没有数据了','data' => $data]);
        }else{
            return response()->json(['code'=>1,'msg'=>'成功','data' => $data]);
        }

    }
    /**
     * 删除项目角色关联
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delCartoonRole(Request $request){
        $data = $request->all();
//        $this->v->validate('delCartoonRole');

        $member_id=Auth::user()->member_id;

        $CartoonRoles=new CartoonRoles();
        $cartoon_id = $CartoonRoles->where(['id'=>$data['id']])->value('cartoon_id');
        if (empty($cartoon_id)){
            return response()->json(['code'=>0,'msg'=>'角色不存在']);
        }
        $CartoonMembers=new CartoonMembers();
        $CartoonMembers = $CartoonMembers->getData(['member_id'=>$member_id,'cartoon_id'=>$cartoon_id]);
        if (empty($CartoonMembers)){
            return response()->json(['code'=>0,'msg'=>'您没有权限删除角色']);
        }

        $rs = $CartoonRoles->where(['id'=>$data['id']])->delete();
        if ($rs){
            return response()->json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return response()->json(['code'=>0,'msg'=>'操作失败']);

        }
    }


    public function myCartoonRoleList(Request $request){
//        $member_id=Auth::user()->member_id;
        $member_id=1;
        $info=$request->input();
        $info['page'] = $request->input('page',ConstCode::PAGE);
        $info['pageSize']  = $request->input('pageSize',ConstCode::PER_PAGE);
        $info['username']  = $request->input('username','');
        $list=$this->character->getList(['uid'=>$member_id],$info);
        if (empty($list['list'])){
            return response()->json(['code'=>0,'msg'=>'没有数据了']);
        }else{
            return response()->json(['code'=>1,'msg'=>'成功','data' => $list]);
        }

    }
}
